using System.Data.SqlClient;
using NUnit.Framework;

namespace DatabaseUnitTesting.Tests
{
    [TestFixture]
    public class DatabaseUnitTesterPerformanceTests
    {
        private SqlConnection connection;
        private SqlTransaction transaction;
        private string database;
        private string snapshot;
        private string server;
        private DatabaseUnitTester unitTests;
        private string filename;

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            database = "astyler";
            snapshot = "tempSS";
            server = "(local)";
            string connectionString = "Application Name=UnitTests;Trusted_Connection=true;Server=" + server +
                                      ";Database=" + database;
            connection =
                new SqlConnection(connectionString);
            connection.Open();
            unitTests = new DatabaseUnitTester(connection, database, snapshot);

            transaction = unitTests.BeginTestTransaction();

            unitTests.AddObjectComparison("dbo", "t100k");
            unitTests.AddColumnToIgnore("dbo", "t100k", "price");

            string commandText = "INSERT INTO t100k (id, c, price) values (-1, 'Unit Test Insert', 99)";
            SqlCommand command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();

            commandText = "UPDATE t100k SET c = 'Unit Test Update' where id = 1";
            command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            filename = "C:/Temp/PerformanceTest.txt";

            unitTests.WriteDiffsToXml(filename);

            unitTests.EndTestTransaction();
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            unitTests.Dispose();
            connection.Close();
        }

        [SetUp]
        public void SetUp()
        {
            transaction = unitTests.BeginTestTransaction();
        }

        [TearDown]
        public void TearDown()
        {
            unitTests.EndTestTransaction();
        }

        [Test]
        public void PerformanceTest0()
        {
            unitTests.AddObjectComparison("dbo", "t100k");
            unitTests.AddColumnToIgnore("dbo", "t100k", "price");
            string commandText = "INSERT INTO t100k (id, c, price) values (-1, 'Unit Test Insert', 99)";
            SqlCommand command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            commandText = "UPDATE t100k SET c = 'Unit Test Update' where id = 1";
            command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            Assert.IsTrue(unitTests.CompareDiffsToXml(filename));
        }

        [Test]
        public void PerformanceTest1()
        {
            unitTests.AddObjectComparison("dbo", "t100k");
            unitTests.AddColumnToIgnore("dbo", "t100k", "price");
            string commandText = "INSERT INTO t100k (id, c, price) values (-1, 'Unit Test Insert', 99)";
            SqlCommand command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            commandText = "UPDATE t100k SET c = 'Unit Test Update' where id = 1";
            command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            Assert.IsTrue(unitTests.CompareDiffsToXml(filename));
        }

        [Test]
        public void PerformanceTest2()
        {
            unitTests.AddObjectComparison("dbo", "t100k");
            unitTests.AddColumnToIgnore("dbo", "t100k", "price");
            string commandText = "INSERT INTO t100k (id, c, price) values (-1, 'Unit Test Insert', 99)";
            SqlCommand command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            commandText = "UPDATE t100k SET c = 'Unit Test Update' where id = 1";
            command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            Assert.IsTrue(unitTests.CompareDiffsToXml(filename));
        }

        [Test]
        public void PerformanceTest3()
        {
            unitTests.AddObjectComparison("dbo", "t100k");
            unitTests.AddColumnToIgnore("dbo", "t100k", "price");
            string commandText = "INSERT INTO t100k (id, c, price) values (-1, 'Unit Test Insert', 99)";
            SqlCommand command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            commandText = "UPDATE t100k SET c = 'Unit Test Update' where id = 1";
            command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            Assert.IsTrue(unitTests.CompareDiffsToXml(filename));
        }

        [Test]
        public void PerformanceTest4()
        {
            unitTests.AddObjectComparison("dbo", "t100k");
            unitTests.AddColumnToIgnore("dbo", "t100k", "price");
            string commandText = "INSERT INTO t100k (id, c, price) values (-1, 'Unit Test Insert', 99)";
            SqlCommand command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            commandText = "UPDATE t100k SET c = 'Unit Test Update' where id = 1";
            command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            Assert.IsTrue(unitTests.CompareDiffsToXml(filename));
        }

        [Test]
        public void PerformanceTest5()
        {
            unitTests.AddObjectComparison("dbo", "t100k");
            unitTests.AddColumnToIgnore("dbo", "t100k", "price");
            string commandText = "INSERT INTO t100k (id, c, price) values (-1, 'Unit Test Insert', 99)";
            SqlCommand command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            commandText = "UPDATE t100k SET c = 'Unit Test Update' where id = 1";
            command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            Assert.IsTrue(unitTests.CompareDiffsToXml(filename));
        }

        [Test]
        public void PerformanceTest6()
        {
            unitTests.AddObjectComparison("dbo", "t100k");
            unitTests.AddColumnToIgnore("dbo", "t100k", "price");
            string commandText = "INSERT INTO t100k (id, c, price) values (-1, 'Unit Test Insert', 99)";
            SqlCommand command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            commandText = "UPDATE t100k SET c = 'Unit Test Update' where id = 1";
            command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            Assert.IsTrue(unitTests.CompareDiffsToXml(filename));
        }

        [Test]
        public void PerformanceTest7()
        {
            unitTests.AddObjectComparison("dbo", "t100k");
            unitTests.AddColumnToIgnore("dbo", "t100k", "price");
            string commandText = "INSERT INTO t100k (id, c, price) values (-1, 'Unit Test Insert', 99)";
            SqlCommand command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            commandText = "UPDATE t100k SET c = 'Unit Test Update' where id = 1";
            command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            Assert.IsTrue(unitTests.CompareDiffsToXml(filename));
        }

        [Test]
        public void PerformanceTest8()
        {
            unitTests.AddObjectComparison("dbo", "t100k");
            unitTests.AddColumnToIgnore("dbo", "t100k", "price");
            string commandText = "INSERT INTO t100k (id, c, price) values (-1, 'Unit Test Insert', 99)";
            SqlCommand command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            commandText = "UPDATE t100k SET c = 'Unit Test Update' where id = 1";
            command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            Assert.IsTrue(unitTests.CompareDiffsToXml(filename));
        }

        [Test]
        public void PerformanceTest9()
        {
            unitTests.AddObjectComparison("dbo", "t100k");
            unitTests.AddColumnToIgnore("dbo", "t100k", "price");
            string commandText = "INSERT INTO t100k (id, c, price) values (-1, 'Unit Test Insert', 99)";
            SqlCommand command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            commandText = "UPDATE t100k SET c = 'Unit Test Update' where id = 1";
            command = new SqlCommand(commandText, connection);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
            Assert.IsTrue(unitTests.CompareDiffsToXml(filename));
        }
    }
}
